<?php
namespace Common\Behaviors;
class boxphpBehavior extends \Think\Behavior
{
    //行为执行入口
    public function run(&$param)
    {
        $this->BoxRun();
    }


    public function BoxRun()
    {
        /******自定义的路由****/
        $this->set_session();
        include(COMMON_PATH . 'Common/functions.php');
        $this->BoxRoute();
        include(COMMON_PATH . 'Library/BaseController.class.php');
        include(COMMON_PATH . 'Library/BaseModel.class.php');
        include(COMMON_PATH . 'Library/AdminController.class.php');
        include(COMMON_PATH . 'Library/HomeController.class.php');
        include(COMMON_PATH . 'Library/Addon.class.php');
        include(COMMON_PATH . 'Library/LogicController.class.php');
        include(COMMON_PATH . 'Library/HooksController.class.php');
        include(COMMON_PATH . 'Library/WidgetController.class.php');
    }

    public function set_session()
    {
        @$session_id = $_COOKIE['session_id'];
        $options = C('SESSION_OPTIONS');
        if (!$session_id) {
            C('SESSION_OPTIONS', $options);
            session(C('SESSION_OPTIONS'));
            session_start();
            $session_id = session_id();
            setcookie("session_id", $session_id, time() + 3600 * 24 * 14, "/", "." . DomainName);//同一个域名下同一个，保持14天会话
        } else {
            $options['id'] = $session_id;
            C('SESSION_OPTIONS', $options);
            session(C('SESSION_OPTIONS'));
            session_start();
        }

    }

    public function BoxRoute()
    {
        /*$depr   =   C('URL_PATHINFO_DEPR');
        @$paths  =   explode($depr,trim($_SERVER['PATH_INFO'],$depr));
        $rules      = C('APP_SUB_DOMAIN_RULES');
        $domain = array_slice(explode('.', $_SERVER['HTTP_HOST']), 0, -2);
        $subDomain = implode('.', $domain);
        if(isset($rules[$subDomain])) { // 子域名
             $rule = $rules[$subDomain];
        }*/
        $depr = C('URL_PATHINFO_DEPR');
        $PATH_INFO = explode("?", $_SERVER['REQUEST_URI']);
        @$paths = explode($depr, trim($PATH_INFO[0], $depr));
        $rules = C('APP_SUB_DOMAIN_RULES');
        $domain = array_slice(explode('.', $_SERVER['HTTP_HOST']), 0, -2);
        /*****泛解析**/
        $count = count($domain);
        for ($i = 0; $i < $count; $i++) {
            $data = $domain;
            $data[$i] = "*";
            $subDomains[] = $data;
        }
        $subDomains[] = $domain;
        foreach ($subDomains as $domain) {
            $subDomainlist[] = implode('.', $domain);
        }
        foreach ($subDomainlist as $subDomain) {
            if (isset($rules[$subDomain])) { // 子域名
                $rule = $rules[$subDomain];
            }
        }

        /***********判断是否使用一级层*********/
        $projectName = ucfirst($paths[0]);
        $domains = C('DOMAIN_LEVEL_1');
        $apps = C('APP_LEVEL_1');
        if (in_array($projectName, $apps)) {
            C("MODULE_ALLOW_LIST", array($projectName));
            C("DEFAULT_MODULE", $projectName);
            C("CONTROLLER_LEVEL", 1);//是使用一层目录的
            C("APP_SUB_DOMAIN_DEPLOY", 0);
            C("APP_SUB_DOMAIN_RULES", array());
            define("APP_URL_TYPE", 1);
            define('BOX_MODULE_NAME', $projectName);
            return;
        }
        /****************通过目录访问****/
        $boxApp = getApps();
        if (in_array($projectName, $boxApp)) {
            C("CONTROLLER_LEVEL", 2);
            C("MODULE_ALLOW_LIST", array($projectName));
            C("DEFAULT_MODULE", $projectName);
            C("APP_SUB_DOMAIN_DEPLOY", 0);
            C("APP_SUB_DOMAIN_RULES", array());
            $array = explode('/', trim($PATH_INFO[0], "/"));
            // 模块绑定
            define('BOX_MODULE_NAME', $array[1] ? $array[1] : "Home");
            //echo 123;
            C('DEFAULT_V_LAYER', "View/" . BOX_MODULE_NAME);
            define("APP_URL_TYPE", 2);
            return;
        }


        $is_level_1 = 0;//使用一层级域名访问模式
        foreach ($subDomainlist as $one) {
            if ($domains[$one]) $is_level_1 = $domains[$one];
        }
        if ($is_level_1 && !in_array($projectName, $apps)) {
            $projectName = $is_level_1;
            C("MODULE_ALLOW_LIST", array($projectName));
            C("DEFAULT_MODULE", $projectName);
            C("CONTROLLER_LEVEL", 1);//是使用一层目录的
            C("APP_SUB_DOMAIN_DEPLOY", 1);
            C("APP_SUB_DOMAIN_RULES", array($subDomain => $projectName));
            define("APP_URL_TYPE", 3);
            return;
        }

        /************使用子域名时路由且是二层级项目模式，路由简化成和一层级的一样啊！！！******/
        if (C('APP_SUB_DOMAIN_DEPLOY') && C("CONTROLLER_LEVEL") == 2) {
            if (!empty($rule)) {
                // 子域名部署规则 '子域名'=>array('模块名[/控制器名]','var1=a&var2=b');
                if (is_array($rule)) {
                    @list($rule, $vars) = $rule;
                }
                $array = explode('/', $rule);
                // 模块绑定
                define('BIND_MODULE', array_shift($array));
                // 控制器绑定
                if (!empty($array)) {
                    /******目前只支持二层下****/
                    if (C('CONTROLLER_LEVEL') == 2) {
                        $m = $array[0];
                        $c = $paths['0'] ? $paths['0'] : C('DEFAULT_CONTROLLER');
                    }
                    $dm = BIND_MODULE ? BIND_MODULE : C('DEFAULT_MODULE');
                    C('DEFAULT_MODULE', $dm);
                    define('CONTROLLER_PATH', "{$dm}\Controller\\" . $m);
                    define('BOX_MODULE_NAME', $m);
                    define('BOX_PHP', 1);
                    define('CONTROLLER_NAME', $c);
                }
            }
            $action = $paths['1'] ? $paths['1'] : C('DEFAULT_ACTION');
            define('BIND_ACTION', $action);
            define('ACTION_NAME', $action);
            define("APP_URL_TYPE", 3);
            C('VIEW_PATH', './Apps/' . BIND_MODULE . '/View/' . BOX_MODULE_NAME . '/');
            return;
        }
    }

}
